package com.demohomework.homeworkdemo.controller;



import com.demohomework.homeworkdemo.pojo.AssSubmit;
import com.demohomework.homeworkdemo.pojo.Result;
import com.demohomework.homeworkdemo.service.AssSubmitService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Objects;

@RestController
@RequestMapping("/assSubmit")
public class AssSubmitController {
    @Autowired
    private AssSubmitService assSubmitService;

    //新增
    @PostMapping("/add")
    public Result add(@RequestBody @Validated({AssSubmit.Add.class}) AssSubmit assSubmit) {
        if(Objects.equals(assSubmit.getInfo(), "")){
            return Result.error("作业信息不能为空");
        }
        assSubmitService.add(assSubmit);
        return Result.success();
    }

    //查询作业
    @GetMapping("/list")
    public Result<List<AssSubmit>> list() {
        List<AssSubmit> assignments = assSubmitService.list();
        return Result.success(assignments);
    }
    //根据id查询作业
    @GetMapping("/select/{id}")
    public Result<AssSubmit> show( @PathVariable Integer id) {
        AssSubmit a = assSubmitService.findId(id);
        return Result.success(a);
    }
    //根据作业id查询学生作业
    @GetMapping("/{assId}")
    public Result<List<AssSubmit>> search(@PathVariable("assId") Integer assId) {
        List<AssSubmit> a = assSubmitService.search(assId); // 根据提供的条件进行查询
        return Result.success(a);
    }
    //根据学生id查询学生作业
    @GetMapping("/selectByStuId/{stuId}")
    public Result<List<AssSubmit>> searchByStudentId(@PathVariable("stuId") Integer stuId) {
        List<AssSubmit> a = assSubmitService.searchByStudentId(stuId); // 根据提供的条件进行查询
        return Result.success(a);
    }

    //更新作业
    @PutMapping("/update")
    public Result update(@RequestBody @Validated(AssSubmit.Update.class) AssSubmit assSubmit){
        assSubmitService.update(assSubmit);
        return Result.success();
    }
    //删除作业
    @DeleteMapping("/delete")
    public Result delete(@RequestBody AssSubmit assSubmit){
        assSubmitService.delete(assSubmit);
        return Result.success();
    }
}
